//
//  CollectionVeiw.m
//  FKGW
//
//  Created by song ce on 2017/2/16.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "CollectionVeiw.h"
#import "ShopCollectionCell.h"
#import "ShopDetailViewController.h"

@implementation CollectionVeiw
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self _initSubviews];
    }
    return self;
}


- (void)_initSubviews{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 8;
    //设置滑动方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    UICollectionView *collectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 187) collectionViewLayout:layout];
    //设置分页
    collectionV.bounces = YES;
    collectionV.showsHorizontalScrollIndicator = NO;
    [self  addSubview:collectionV];
    collectionV.backgroundColor = [UIColor whiteColor];
    //设置代理
    collectionV.delegate = self;
    collectionV.dataSource = self;
    //注册
    [collectionV registerNib:[UINib nibWithNibName:@"ShopCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"ShopCollectionCell"];

    _collectionV = collectionV;
}

-(void)setDataSource:(NSArray *)dataSource{
    _dataSource = dataSource;
    [_collectionV reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;{
    return self.dataSource.count;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    UIEdgeInsets edge = UIEdgeInsetsMake(15, 8, 0 ,9);
    return edge;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;{
    ShopCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ShopCollectionCell" forIndexPath:indexPath];
    ShopCellModel *model = self.dataSource[indexPath.row];
    cell.model = model;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(130, 192);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
     ShopCellModel *model = self.dataSource[indexPath.row];
    ShopDetailViewController  *shopDetail = [[ShopDetailViewController alloc]init];
    shopDetail.vcName = model.marketName;
    shopDetail.marketId =model.marketId;
      shopDetail.hidesBottomBarWhenPushed = YES;
    [[self secondNavigationController] pushViewController:shopDetail animated:YES];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
      [[NSNotificationCenter defaultCenter]postNotificationName:@"otherScrollDisenabled" object:nil];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;  {
    if(scrollView.contentOffset.x+scrollView.width ==scrollView.contentSize.width){
           [scrollView setContentOffset:CGPointMake(scrollView.contentSize.width-scrollView.width-1, 0) animated:NO];
         }
}

- (BaseNavigationViewController*)secondNavigationController{
    UIResponder *next = self.nextResponder;
    while (next!=nil) {
        if([next isKindOfClass:[BaseNavigationViewController class]]){
            return (BaseNavigationViewController*)next;
        }
        next = next.nextResponder;
    }
    return nil;
}




@end
